<?php
namespace app\api\controller;
use think\Controller;
use think\Request;

class Code extends Controller
{
    public function index()
    {
    }

    //发送验证码
    public function send()
    {
    	$mobile =$_GET['mobile'];
    	if(empty($mobile)){
    		return json_encode(['errorCode'=>1000,'msg'=>'mobile can not empty']);
    	}

    	$code = rand(1000,9999);

        //发送验证码
        try{

            //存入redis
            $redis = new \Swoole\Coroutine\Redis();
            $redis->connect('127.0.0.1',6379);
            
            $result = $redis->set('sms_'.$mobile,$code,300);
            $redis->close();
            if(!$result){
                return json_encode(['errorCode'=>2000,'msg'=>'redis error']);
            }
            return json_encode(['errorCode'=>0,'msg'=>'send success','data'=>$code]);

        }catch(\Exception $e){

        }  
    }
}
